package com.code.ddbond.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.ddbond.mapper.UserInfoMapper;
import com.code.ddbond.mapper.VideoInfoMapper;
import com.code.ddbond.pojo.UserInfo;
import com.code.ddbond.pojo.VideoInfo;
import com.code.ddbond.util.MinioTemplate;
import com.code.ddbond.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class VideoInfoServiceImpl extends ServiceImpl<VideoInfoMapper, VideoInfo> implements VideoInfoService
{
    @Autowired
    private MinioTemplate minioTemplate;

    @Autowired
    private VideoInfoMapper videoInfoMapper;

    @Override
    @Transactional
    public void uploadVideo(VideoVo videoVo) {
        // 上传文件到 MinIO
        String fileName = System.currentTimeMillis() + "_" + videoVo.getFile().getOriginalFilename();
        minioTemplate.upload(videoVo.getFile(), fileName);

        // 保存视频信息到数据库
        VideoInfo videoInfo = new VideoInfo();
        videoInfo.setTitle(videoVo.getTitle());
        videoInfo.setFkUserId(videoVo.getFkUserId());
        videoInfo.setVideoUrl(fileName);
        videoInfo.setPublishTime(new Date());
        videoInfo.setOpenStatus(videoVo.getOpenStatus());
        videoInfo.setRemark(videoVo.getRemark());

        videoInfoMapper.insert(videoInfo);
    }

    @Override
    public Page<VideoInfo> searchVideos(String keyword, int pageNum, int pageSize)
    {
        QueryWrapper<VideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword);
        Page<VideoInfo> page = new Page<>(pageNum, pageSize);
        return this.page(page, queryWrapper);
    }
}
